#ifndef __APP_OCPP_UTIL_H__
#define __APP_OCPP_UTIL_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <rtthread.h>
#include "os_types.h"
#include <sys/time.h>
/*  ocpp message type ID */
typedef enum {
    E_OCPP_MESSAGE_NONE = 0,
    E_OCPP_MESSAGE_CALL = 2,        // 客户端到服务端请求
    E_OCPP_MESSAGE_CALL_RESULT = 3, // 服务端到客户端正常应答
    E_OCPP_MESSAGE_CALL_ERROR = 4,  // 服务端到客户端错误应答
} app_ocpp_message_type_e;

// Operations Initiated by Charge Point 上行数据
typedef enum {
    E_OCPP_CONNECT = 0,                     // 连接服务器
    E_OCPP_AUTHORIZE,                       // 授权认证(Core)
    E_OCPP_BOOT_NOTIFICATION,               // 启动通知(Core)
    E_OCPP_DATA_TRANSFER_CLINET,            // 设备发送自定义数据给平台(非OCPP定义)
    E_OCPP_DIAGNOSTICS_STATUS_NOTIFICATION, // 诊断状态通知(Firmware Management)
    E_OCPP_FIRMWARE_STATUS_NOTIFICATION,    // 固件状态通知(Firmware Management)
    E_OCPP_HEARTBEAT,                       // 心跳(Core)
    E_OCPP_METER_VALUES,                    // 仪表值(Core)
    E_OCPP_START_TRANSACTION,               // 开始传输(Core)
    E_OCPP_STATUS_NOTIFICATION,             // 状态通知(Core)
    E_OCPP_STOP_TRANSACTION,                // 停止传输(Core)

    E_OCPP_SEND_ACTION_MAX
} app_ocpp_send_action_e;

// Operations Initiated by Central System  下行数据
typedef enum {
    E_OCPP_CANCEL_RESERVATION = 0,   // 取消预订(Remote Trigger)
    E_OCPP_CHANGE_AVAILABILITY,      // 改变可用性(Core)
    E_OCPP_CHANGE_CONFIGURATION,     // 改变配置(Core)
    E_OCPP_CLEAR_CACHE,              // 清除缓存(Core)
    E_OCPP_CLEAR_CHARGING_PROFILE,   // 清除充电配置文件(Reservation)
    E_OCPP_DATA_TRANSFER_SERVER,     // 平台发送自定义数据给设备(Core)
    E_OCPP_GET_COMPOSITE_SCHEDULE,   // 获取综合计划(Reservation)
    E_OCPP_GET_CONFIGURATION,        // 获取配置(Core)
    E_OCPP_GET_DIAGNOSTICS,          // 获取诊断(Firmware Management)
    E_OCPP_GET_LOCAL_LIST_VERSION,   // 获取本地列表版本(Local Auth List Management)
    E_OCPP_REMOTE_START_TRANSACTION, // 远程启动(Core)
    E_OCPP_REMOTE_STOP_TRANSACTION,  // 远程停止(Core)
    E_OCPP_RESERVE_NOW,              // 预订(Remote Trigger)
    E_OCPP_RESET,                    // 重启(Core)
    E_OCPP_SEND_LOCAL_LIST,          // 发送本地列表(Local Auth List Management)
    E_OCPP_SET_CHARGING_PROFILE,     // 设置充电配置文件(Reservation)
    E_OCPP_TRIGGER_MESSAGE,          // 触发消息(Smart Charging)
    E_OCPP_UNLOCK_CONNECTOR,         // 解锁连接器(Core)
    E_OCPP_UPDATE_FIRMWARE,          // 更新固件(Firmware Management)
    E_OCPP_RECV_ACTION_MAX
} app_ocpp_recv_action_e;

typedef enum {
    E_DATA_TRANSFER_AUTHORIZE,
    E_DATA_TRANSFER_GET_15118EV_CERTIFICATE,
    E_DATA_TRANSFER_GET_CERTIFICATE_STATUS,
    E_DATA_TRANSFER_SIGN_CERTIFICATE,
    E_DATA_TRANSFER_MAX,
} ocpp_data_transfer_message_e;

typedef enum {
    E_DATA_TRANSFER_CERTIFICATE_SIGNED,
    E_DATA_TRANSFER_DELETE_CERTIFICATE,
    E_DATA_TRANSFER_GET_INSTALLED_CERTIFICATEIDS,
    E_DATA_TRANSFER_INSTALL_CERTIFICATE,
    E_DATA_TRANSFER_UPDATE_15118EV_CERTIFICATE,
    E_DATA_TRANSFER_EXTENDED_TRIGGER_MESSAGE,
#ifdef A5158_EDITION
    E_DATA_TRANSFER_SET_DESPLAY_IMAGE,
#endif
    E_DATA_TRANSFER_SERVER_MAX,
} ocpp_data_transfer_server_message_e;

typedef enum {
    E_DATA_TRANSFER_IMAGEID_DEFAULT_PRICE_TICKER,
    E_DATA_TRANSFER_IMAGEID_DEFAULT_PRICE,
    E_DATA_TRANSFER_IMAGEID_USER_PRICE,
    E_DATA_TRANSFER_IMAGEID_RUNNING_COST,
    E_DATA_TRANSFER_IMAGEID_FINAL_COST,
    E_DATA_TRANSFER_IMAGEID_ERROR_MESSAGE,
    E_DATA_TRANSFER_IMAGEID_MAX,
} ocpp_data_transfer_image_id_e;

// åº”ç­”?„å­—ç¬?¸²ç±»åž‹
typedef enum {
    E_OCPP_RESP_ACCEPTED, // Accepted
    E_OCPP_RESP_REJECTED, // Rejected
    E_OCPP_RESP_SCHEDULED,
    E_OCPP_RESP_REBOOT_REQUIRED,
    E_OCPP_RESP_NOT_SUPPORTED,
    E_OCPP_RESP_UNKNOW,
    E_OCPP_RESP_FAULTED,
    E_OCPP_RESP_OCCUPIED,
    E_OCPP_RESP_UNAVAILABLE,
    E_OCPP_RESP_FAILED,
    E_OCPP_RESP_VERSION_MISMATCH,
    E_OCPP_RESP_NOT_IMPLEMENTED,
    E_OCPP_RESP_UNLOCKED,
    E_OCPP_RESP_UNLOCK_FAILED,
    E_OCPP_RESP_MAX,
} app_ocpp_resp_result_e;

/******** Operations Initiated by Charge Point commands STRAT ************/
#define OCPP_AUTHORIZE "Authorize"

/* BootNotification */
#define OCPP_BOOTNOTIFICATION "BootNotification"
#ifdef OCPP_BOOTNOTIFICATION
/* BootNotification.req */
#define OCPP_CHARGEBOXSERIAL "chargeBoxSerialNumber"
#define OCPP_CHARGEPOINTMODEL "chargePointModel"
#define OCPP_CHARGEPOINTSERIAL "chargePointSerialNumber"
#define OCPP_CHARGEPOINTVENDOR "chargePointVendor"
#define OCPP_FIRMWAREVERSION "firmwareVersion"
#define OCPP_ICCID "iccid"
#define OCPP_IMSI "imsi"
#define OCPP_METERSERIALNUMBER "meterSerialNumber"
#define OCPP_METERTYPE "meterType"
/* BootNotification.conf */
#define OCPP_CURRENTTIME "currentTime"
#define OCPP_INTERVAL "interval"
#endif /* OCPP_BOOTNOTIFICATION */

#define OCPP_DIAGNOSTICSSTATUSNOTIFICATION "DiagnosticsStatusNotification"
#define OCPP_FIRMWARESTATUSNOTIFICATION "FirmwareStatusNotification"
#define OCPP_HEARTBEAT "Heartbeat"

/* MeterValues.req */
#define OCPP_METERVALUES "MeterValues"
#define OCPP_METERVALUE "meterValue"
#define OCPP_METERSTOP "meterStop"

/* StartTransaction.req */
#define OCPP_STARTTRANSACTION "StartTransaction"
#define OCPP_METERSTART "meterStart"

/* StatusNotification.req */
#define OCPP_STATUSNOTIFICATION "StatusNotification"
#define OCPP_ERRORCODE "errorCode"

#define OCPP_ERROR_NOTIMPLEMENTED "NotImplemented"
#define OCPP_ERROR_NOTSUPPORTED "NotSupported"
#define OCPP_ERROR_PROTOCOL "ProtocolError"
#define OCPP_ERROR_SECURITY "SecurityError"
#define OCPP_ERROR_FORMATION "FormationViolation"
#define OCPP_ERROR_PROPERTYCONSTRAINT "PropertyConstraintViolation"
#define OCPP_ERROR_OCCURENCECONSTRAINT "OccurenceConstraintViolation"
#define OCPP_ERROR_TYPECONSTRAINT "TypeConstraintViolation"
#define OCPP_ERROR_GENERIC "GenericError"

#define OCPP_INFO "info"

#define OCPP_STOPTRANSACTION "StopTransaction"
/******** Operations Initiated by Charge Point commands END ************/

/******** Operations Initiated by Central System commands STRAT ************/
/* Reservation */
#define OCPP_CANCELRESERVATION "CancelReservation"
#define OCPP_RESERVATIONID "reservationId"

/* Availability */
#define OCPP_CHANGEAVAILABILITY "ChangeAvailability"
#define OCPP_INOPERATIVE "Inoperative"
#define OCPP_OPERATIVE "Operative"

/* Configuration */
/* ChangeConfiguration.req */
#define OCPP_CHANGECONFIGURATION "ChangeConfiguration"
#define OCPP_KEY "key"
#define OCPP_VALUE "value"
#define OCPP_GETCONFIGURATION "GetConfiguration"
#define OCPP_CONFIGURATIONKEY "configurationKey"
/* Configuration */

#define OCPP_CLEARCACHE "ClearCache"

#define OCPP_DATATRANSFER "DataTransfer"
#define OCPP_GETCOMPOSITESCHEDULE "GetCompositeSchedule"

#define OCPP_GETDIAGNOSTICS "GetDiagnostics"
#define OCPP_FILENAME "fileName"

/* LocalList */
/* GetLocalListVersion.req */
#define OCPP_GETLOCALLISTVERSION "GetLocalListVersion"
#define OCPP_LISTVERSION "listVersion"
/* SendLocalList.req */
#define OCPP_SENDLOCALLIST "SendLocalList"
#define OCPP_LOCALAUTHORIZATIONLIST "localAuthorizationList"
#define OCPP_UPDATETYPE "updateType"

#define OCPP_DIFFERENTIAL "Differential"
#define OCPP_FULL "Full"
#define OCPP_RAW "Raw"
#define OCPP_SIGNEDDATA "SignedData"

#define OCPP_REMOTESTARTTRANSACTION "RemoteStartTransaction"
#define OCPP_REMOTESTOPTRANSACTION "RemoteStopTransaction"

/* ReserveNow */
#define OCPP_RESERVENOW "ReserveNow"
#define OCPP_EXPIRYDATE "expiryDate"
#define OCPP_PARENTIDTAG "parentIdTag"

/* ResetType */
#define OCPP_RESET "Reset"
#define OCPP_HARD "Hard"
#define OCPP_SOFT "Soft"

/* ChargingProfile start */
/* SetChargingProfile.req */
#define OCPP_SETCHARGINGPROFILE "SetChargingProfile"
#define OCPP_CSCHARGINGPROFILE "csChargingProfiles"
#define OCPP_CHARGINGPROFILEID "chargingProfileId"
#define OCPP_TRANSACTIONID "transactionId"
#define OCPP_STACKLEVEL "stackLevel"
#define OCPP_CHARGINGPROFILEPURPOSE "chargingProfilePurpose"
#define OCPP_CHARGINGPROFILEKIND "chargingProfileKind"
#define OCPP_RECURRENCYKIND "recurrencyKind"
#define OCPP_VALIDFROM "validFrom"
#define OCPP_VALIDTO "validTo"
/* ClearChargingProfile.req */
#define OCPP_CLEARCHARGINGPROFILE "ClearChargingProfile"
#define OCPP_ID "id"

/* ChargingSchedule */
#define OCPP_CHARGINGSCHEDULE "chargingSchedule"
#ifdef OCPP_CHARGINGSCHEDULE
#define OCPP_DURATION "duration"
#define OCPP_STARTSCHEDULE "startSchedule"
#define OCPP_CHARGINGRATEUNIT "chargingRateUnit"
#define OCPP_CHARGINGSCHEDULEPERIOD "chargingSchedulePeriod"
#ifdef OCPP_CHARGINGSCHEDULEPERIOD
#define OCPP_STARTPERIOD "startPeriod"
#define OCPP_LIMIT "limit"
#define OCPP_NUMBERPHASES "numberPhases"
#endif /* OCPP_CHARGINGSCHEDULEPERIOD */
#define OCPP_MINCHARGINGRATE "minChargingRate"
#endif /* OCPP_CHARGINGSCHEDULE */

#define OCPP_CHARGINGPROFILE "chargingProfile"
#define OCPP_CSCHARGINGPROFILES "csChargingProfiles"
/* ChargingProfile end */

#define OCPP_TRIGGERMESSAGE "TriggerMessage"
#define OCPP_UNLOCKCONNECTOR "UnlockConnector"
#define OCPP_UPDATEFIRMWARE "UpdateFirmware"
/******** Operations Initiated by Central System commands END ************/

/* Status */
#define OCPP_STATUS "status"
#define OCPP_STATUS_ACCEPTED "Accepted"
#define OCPP_STATUS_PENDING "Pending"
#define OCPP_STATUS_REJECTED "Rejected"
#define OCPP_STATUS_FAULTED "Faulted"
#define OCPP_STATUS_OCCUPIED "Occupied"
#define OCPP_STATUS_UNAVAILABLE "Unavailable"
#define OCPP_STATUS_NOTIMPLEMENTED "NotImplemented"
#define OCPP_STATUS_NOTSUPPORTED "NotSupported"
#define OCPP_STATUS_UNLOCKED "Unlocked"
#define OCPP_STATUS_UNLOCKFAILED "UnlockFailed"
#define OCPP_STATUS_FAILED "Failed"
#define OCPP_STATUS_VERSIONMISMATCH "VersionMismatch"
#define OCPP_STATUS_UNKNOWN "Unknown"
#define OCPP_BLOCKED "Blocked"
#define OCPP_EXPIRED "Expired"
#define OCPP_INVALID "Invalid"
#define OCPP_CONCURRENTTX "ConcurrentTx"
#define OCPP_SCHEDULED "Scheduled"

/* DEFINES FOR ERRORCODE */

/* SampledValue */
#define OCPP_SAMPLEDVALUE "sampledValue"
#ifdef OCPP_SAMPLEDVALUE
#define OCPP_VALUE "value"
#define OCPP_CONTEXT "context"
#define OCPP_FORMAT "format"
#define OCPP_MEASURAND "measurand"
#define OCPP_PHASE "phase"
#define OCPP_LOCATION "location"
#define OCPP_UNIT "unit"

#endif /* OCPP_SAMPLEDVALUE */

/* Common string */
#define OCPP_EMPTY_JSON "{}"
#define OCPP_IDTAG "idTag"
#define OCPP_IDTAGINFO "idTagInfo"
#define OCPP_VENDORID "vendorId"
#define OCPP_MESSAGEID "messageId"
#define OCPP_CONNECTORID "connectorId"
#define OCPP_TYPE "type"
#define OCPP_TIMESTAMP "timestamp"

#define OCPP_REBOOTREQUIRED "RebootRequired"
#define OCPP_TRANSACTIONDATA "transactionData"
#define OCPP_REASON "reason"
#define OCPP_REQUESTEDMESSAGE "requestedMessage"
#define OCPP_VENDORERRORCODE "vendorErrorCode"
#define OCPP_DATA "data"
#define OCPP_ITEMS "items"
#define OCPP_SCHEDULESTART "scheduleStart"
#define OCPP_RETRIES "retries"
#define OCPP_RETRYINTERVAL "retryInterval"
#define OCPP_STARTTIME "startTime"
#define OCPP_STOPTIME "stopTime"
#define OCPP_RETRIEVEDATE "retrieveDate"

// DEFINES FOR FIELD LENGTHS
#define CiString20Type 21
#define CiString25Type 26
#define CiString36Type 37
#define CiString50Type 51
#define CiString255Type 256
#define CiString500Type 501

#define ChargePointVendorType CiString20Type
#define ChargePointModelType CiString20Type
#define IdTokenType CiString20Type
#define DateTimeType CiString20Type
#define VendorType CiString20Type
#define MeterType CiString25Type
#define MeterSerialType CiString25Type
#define IccidType CiString20Type
#define ImsiType CiString20Type
#define FirmwareVersionType CiString50Type
#define ChargePointSerialType CiString25Type
#define ChargeBoxSerialType CiString25Type
#define MessageIdType CiString36Type

// DEFINE FOR OCPP 3RD DATATRANSFER FOR PRICE
#define OCPP_IMAGEID "imageId"
#define OCPP_IMAGEDESCRIPTION "imageDescription"
#define OCPP_INSTRUCTION "instruction"
#define OCPP_PRICESLENGTH "pricesLength"
#define OCPP_PRICES "prices"
#define OCPP_TITLE "title"
#define OCPP_CURRENCYSIGN "currencySign"
#define OCPP_PRICE "price"
#define OCPP_UNIT "unit"
#define OCPP_IDTOKEN "idToken"
#define OCPP_ACCOUNTBLANCE "accountBalance"
#define OCPP_TXID "txId"
#define OCPP_METERVALUE "meterValue"
#define OCPP_COSTCURRENCYSIGN "costCurrenySign"
#define OCPP_COST "cost"
#define OCPP_COSTUNIT "costUnit"
#define OCPP_STATE "state"
#define OCPP_CHARGINGPRICES "chargingPrices"
#define OCPP_IDLEPRICES "idlePrices"
#define OCPP_CHARGINGTIME "chargingTime"
#define OCPP_ERRORMESSAGE "errorMessage"

enum app_ocpp_send_result_e {
    E_OCPP_SEND_RESULT_OK = 0,  // 发送成功
    E_OCPP_SEND_RESULT_ERR,     // 发送失败
    E_OCPP_SEND_RESULT_TIMEOUT, // 发送超时
};
rt_err_t app_ocpp_chage_timestamp_format(char *str_time, time_t *time_c);

rt_err_t app_ocpp_change_local_time(char *timestr, time_t *time_c);

rt_err_t app_ocpp_get_local_time(char *timestr, uint16_t size);

time_t app_ocpp_timestr2timestamp(char *timestr);

rt_err_t app_ocpp_set_local_time(char *timestr);

uint8_t app_ocpp_dev_is_link(void);

BOOL app_ocpp_get_update_time(void);

void app_ocpp_clr_update_time(void);

uint32_t app_ocpp_get_tr_id(void);

rt_err_t app_ocpp_get_idtag(uint8 type, char *addr);

int8_t app_ocpp_get_evse_state(void);

uint32_t app_ocpp_get_meter_value(uint8_t type);

int32_t app_ocpp_get_evse_temp(void);

int32_t app_ocpp_get_evse_error_core(void);

uint8_t app_ocpp_get_evse_end_reason(void);

int8_t app_ocpp_get_ota_state(void);

int8_t app_ocpp_get_diagnostics_state(void);

int16 app_search_util_index(char *str_search, char *str_flag, uint16 index);

void app_util_buf_sub(char *str_in, char *str_out, uint16 u16Out_bgn, int16 s16len);
#ifdef __cplusplus
}
#endif

#endif